//****************************************************************
// Autor:   Gustavo Castro
//       
// Seccion: 10
// Nombre del Archivo: BinaryTree.java
// Fecha de Modificacion: 
// Descripcion:
// Arbol binario. Código obtenido del libro de clase. 
// ***************************************************************

package hojatrabajo6;

import java.util.Iterator;

/**
 *
 * @author cristian
 */
public class BinaryTree{
    protected Object val; // value associated with node
    protected BinaryTree parent; // parent of node
    protected BinaryTree left, right; // children of node
    public static final BinaryTree EMPTY = new BinaryTree();


    public BinaryTree()
    {
	val = null;
	parent = null; left = right = this;
    }

    public BinaryTree(Object value)
    {
	val = value;
	parent = null;
	left = right = EMPTY;
    }
    public BinaryTree(Object value, BinaryTree left, BinaryTree right)
    {
	this(value);
	setLeft(left);
	setRight(right);
    }
    public BinaryTree left()
    {
	return left;
    }
    public BinaryTree right()
    {
	return right;
    }
    public BinaryTree parent()
    {
	return parent;
    }

    public void setLeft(BinaryTree newLeft)
    {
	if (isEmpty()) return;
	if (left.parent() == this) left.setParent(null);
	left = newLeft;
	left.setParent(this);
    }
    public void setRight(BinaryTree newRight)
    {
	if (isEmpty()) return;
	if (right.parent() == this) right.setParent(null);
	right = newRight;
	right.setParent(this);
    }
    protected void setParent(BinaryTree newParent)
    {
	parent = newParent;
    }
    public boolean isEmpty()
    {
	return this == EMPTY;
    }
    public void setValue(Object value)
    {
	val = value;
    }
    public Object value()
    {
	return val;
    }
    
}
